package com.googlecode.tracey.ui.core;

import java.util.List;

import static java.lang.String.format;

public class PlantUmlMarkupGenerator {

    public String collectPlantUmlMarkup(List<TraceyMessage> messages) {
        Markup markup = new Markup();
        for (TraceyMessage message : messages) {
            markup.addMessage(plantUmlMarkup(message));
        }
        return markup.build();
    }

    private String plantUmlMarkup(TraceyMessage message) {
        return String.format("%s ->> %s:<text class=sequence_diagram_clickable sequence_diagram_message_id=%s>%s</text>", message.from(), message.to(),message.messageId().replaceAll("-","_"), message.name());
    }

    private static class Markup {
        StringBuffer plantUmlMarkup = new StringBuffer(format("@startuml%n"));

        private void addMessage(String messageLine) {
            plantUmlMarkup.append(messageLine).append("\n");
        }

        public String build() {
            plantUmlMarkup.append(format("@enduml%n"));
            return plantUmlMarkup.toString();
        }
    }
}
